package freshmanspecial.mredrock.com.welcomefreshman.MyChart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.List;

import freshmanspecial.mredrock.com.welcomefreshman.Bean.CircleChartData;

/**
 * Created by Administrator on 2017/8/4 0004.
 */

public class CircleChart extends View {

    private Paint paint;
    private int paintWidth = 70,strokeWidth = 8;
    private int speed = 2;
    private float progressing = 0;
    private float radius = 0;
    private int startAngle = -90;
    private float center_X,center_Y;
    private List<CircleChartData> list;

    public CircleChart(Context context) {
        super(context);
    }

    public CircleChart(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircleChart(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        paint.setDither(true);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        center_X = getWidth()/2;
        center_Y = getHeight()/2;
        radius = getWidth()/5;
        if(list.size()==0) return;
        for(int i=0;i<list.size();i++){
            if(progressing/360<list.get(i).getProportion()){
                progressing = progressing+speed;
                invalidate();
                drawBackground(canvas,360,list.get(i).getBackgroundColor(),list.get(i).getBackgroundStrokeColor(),radius+i*105);
                drawCircle(canvas,progressing,list.get(i).getCircleColor(),list.get(i).getCircleStrokeColor(),radius+i*105);
                drawText(canvas,(int)(list.get(i).getProportion()*100)+"%",radius+i*105);
            }
            else {
                drawBackground(canvas,360,list.get(i).getBackgroundColor(),list.get(i).getBackgroundStrokeColor(),radius+i*105);
                drawCircle(canvas,list.get(i).getProportion()*360,list.get(i).getCircleColor(),list.get(i).getCircleStrokeColor(),radius+i*105);
                drawText(canvas,(int)(list.get(i).getProportion()*100)+"%",radius+i*105);
            }
        }
    }
    public void setCircleChartData(List<CircleChartData> list){
        this.list = list;
        progressing = 0;
        postInvalidate();
    }
    private void drawCircle(Canvas canvas,float angle,int circleColor,int circleStrokeColor,float radius){
        RectF rectF = new RectF(center_X-radius,center_Y-radius,center_X+radius,center_Y+radius);
        paint.setStrokeWidth(paintWidth+strokeWidth);
        paint.setColor(circleStrokeColor);
        canvas.drawArc(rectF,startAngle,angle,false,paint);
        paint.setStrokeWidth(paintWidth);
        paint.setColor(circleColor);
        canvas.drawArc(rectF,startAngle,angle,false,paint);
    }
    private void drawBackground(Canvas canvas,float angle,int backgroundColor,int backgroundStrokeColor,float radius){
        RectF oval = new RectF(center_X-radius,center_Y-radius,center_X+radius,center_Y+radius);
        paint.setStrokeWidth(paintWidth+strokeWidth);
        paint.setColor(backgroundStrokeColor);
        canvas.drawArc(oval,startAngle,angle,false,paint);
        paint.setStrokeWidth(paintWidth);
        paint.setColor(backgroundColor);
        canvas.drawArc(oval,startAngle,angle,false,paint);
    }
    private void drawText(Canvas canvas,String text,float radius){
        paint.setStrokeWidth(3);
        paint.setTextSize(45);
        canvas.drawText(text,center_X-paint.measureText(text)-40,center_Y-radius+22,paint);
    }
}
